<?php
/**
 * DouPHP
 * --------------------------------------------------------------------------------------------------
 * 版权所有 2013-2015 漳州豆壳网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.douco.com
 * --------------------------------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在遵守授权协议前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 * 授权协议：http://www.douco.com/license.html
 * --------------------------------------------------------------------------------------------------
 * Author: DouCo
 * Release Date: 2015-10-16
 */
define('IN_DOUCO', true);

require (dirname(__FILE__) . '/include/init.php');
// rec操作项的初始化
$rec = $check->is_rec($_REQUEST['rec']) ? $_REQUEST['rec'] : 'default';
$smarty->assign('rec', $rec);
$smarty->assign('ur_here', $_LANG['coupon']);
$smarty->assign('action_link', array ('text' => $_LANG['coupon_add'],'href' => 'coupon.php?rec=add' ));


if ($rec == 'default') {
    
    $sql = "SELECT * FROM " . $dou->table('coupon') . " ORDER BY user_id ASC";
    // 分页
    $page = $check->is_number($_REQUEST['page']) ? $_REQUEST['page'] : 1;
    $page_url = 'coupon.php?1=1';
    $limit = $dou->pager_bysql($sql, 15, $page, $page_url, $get);

    $coupon_list = $dou->get_all($sql.$limit);
    
    // 赋值给模板
    $smarty->assign('cur', 'coupon');
    $smarty->assign('coupon_list', $coupon_list);
    $smarty->display('coupon.htm');
}


elseif ($rec == 'add') {
    $smarty->assign('action_link', array ('text' => $_LANG['return_list'],'href' => 'coupon.php' ));
    $smarty->display('coupon.htm');
}


elseif ($rec == 'del') {
    // 验证并获取合法的ID
    $coupon_id = $check->is_number($_REQUEST['coupon_id']) ? $_REQUEST['coupon_id'] : $dou->dou_msg($_LANG['illegal'], 'coupon.php');
    $coupon_no = $dou->get_one("SELECT coupon_no FROM " . $dou->table('coupon') . " WHERE coupon_id='$coupon_id'");
    
    if (isset($_POST['confirm']) ? $_POST['confirm'] : '') {
        $dou->create_admin_log($_LANG['coupon_del'] . ': ' . $coupon_no);
        $dou->delete($dou->table('coupon'), "coupon_id = $coupon_id", 'coupon.php');
    } else {
        $_LANG['del_check'] = preg_replace('/d%/Ums', $coupon_no, $_LANG['del_check']);
        $dou->dou_msg($_LANG['del_check'], 'coupon.php', '', '30', "coupon.php?rec=del&coupon_id=$coupon_id");
    }
} 
